package com.cupster.android_x_frame;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

public class ConfirmDialog extends DialogFragment {

    public static final String KEY_NAME ="NAME";
    public static final String KEY_IMG ="NAME";

    View mView;
    ImageView mImg;
    EditText mInput;
    Button mBtn;

    View.OnClickListener mConfirmListener;

    public  static  ConfirmDialog build( String name ,Bitmap bitmap){
        ConfirmDialog dialog = new ConfirmDialog();
        Bundle bundle = new Bundle();
        bundle.putString(KEY_NAME ,name);
        bundle.putParcelable(KEY_IMG ,bitmap);
        dialog.setArguments(bundle);
        return dialog;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mView == null) {
            mView = inflater.inflate(R.layout.dialog_confirm_gesture, container, false);
        }
        setStyle(STYLE_NORMAL, R.style.ConfirmDialogStyle);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        return mView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        mImg = mView.findViewById(R.id.img);
        mBtn = mView.findViewById(R.id.btn_submit);
        mInput = mView.findViewById(R.id.input);

        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mConfirmListener != null){
                    view.setTag( mInput.getText().toString() );
                    mConfirmListener.onClick(view);
                }
                dismiss();
            }
        });

        initData();
    }

    private void initData() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            String name = bundle.getString(KEY_NAME);
            if (!TextUtils.isEmpty(name)){
                mInput.setText(name);
            }
            Bitmap bitmap = bundle.getParcelable(KEY_IMG);
            if (bitmap != null){
                mImg.setImageBitmap(bitmap);
            }else {
                Log.e("ConfirmDialog","bitmap == null");
            }
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        window.setDimAmount(0.2f);//设置亮度
        window.setGravity(Gravity.CENTER);
        window.setWindowAnimations(R.style.PopupAnimation);
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = (int) (0.85*deviceWidth(getContext()));
        window.setLayout(layoutParams.width, layoutParams.height);
        window.setBackgroundDrawableResource(android.R.color.transparent);
    }

    public static int deviceWidth(Context context) {
        return context.getResources().getDisplayMetrics().widthPixels;
    }

    public void setConfirmListener(View.OnClickListener listener){
        mConfirmListener = listener;
    }
}
